//
//  ViewPager.swift
//  navigation
//
//  Created by tuwan on 2018/1/30.
//  Copyright © 2018年 czmc .Inc. All rights reserved.
//

import UIKit
private let identifier = "cell_identifier"
protocol  ViewPagerDelegate : class{
    func onViewPagerChange(currentIndex:Int,duration:CGFloat)
}
class ViewPager:UIView{
    private var lastX : CGFloat = 0;
    private weak var parentVc: UIViewController?
    private let childVcs:[UIViewController]
    private weak var delegate:ViewPagerDelegate?
    private lazy var collectionView : UICollectionView = {
        let layout  = UICollectionViewFlowLayout();
        layout.itemSize = self.bounds.size
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = .horizontal
        let view = UICollectionView(frame:CGRect.zero,collectionViewLayout:layout)
        view.showsHorizontalScrollIndicator=false
        view.bounces = false
        view.isPagingEnabled = true
        view.dataSource = self
        view.register(UICollectionViewCell.self, forCellWithReuseIdentifier: identifier)
        return view
    }()
    init(frame: CGRect,childVcs:[UIViewController],parentVc:UIViewController) {
        self.parentVc = parentVc;
        self.childVcs = childVcs;
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
}

extension ViewPager{
    func setUI(){
        collectionView.frame = self.bounds
        collectionView.delegate = self
        addSubview(collectionView)
    }
    func setViewPagerDelegate(delegate:ViewPagerDelegate){
        self.delegate = delegate
    }
}


extension ViewPager : UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return childVcs.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //创建cell
        let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
        //移除之前的缓存
        for view in cell.contentView.subviews{
            view.removeFromSuperview()
        }
        //给cell 设置内容
        let childView =  childVcs[indexPath.item]
        childView.view.frame = cell.contentView.bounds
        cell.contentView.addSubview(childView.view)
        return cell
    }
    
}
extension ViewPager :UICollectionViewDelegate{
  
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
         var currentIndex=0
        if(scrollView.contentOffset.x>lastX){//left
             currentIndex =  Int(ceil(scrollView.contentOffset.x / collectionView.frame.width))
          
        }else{//right
             currentIndex = Int(floor(scrollView.contentOffset.x / collectionView.frame.width))
        }
     
        if(delegate != nil){
            print(currentIndex,scrollView.contentOffset.x)
            var duration = scrollView.contentOffset.x/(CGFloat(childVcs.count)*collectionView.frame.width)
            delegate?.onViewPagerChange(currentIndex: currentIndex, duration: duration)
        }
        lastX = scrollView.contentOffset.x
    }
    
}

extension ViewPager:ViewPagerIndicatorDelegate{
    func getViewPager() -> ViewPager {
        return self
    }
    
    func onTitleClick(currentIndex: Int) {
        let offsetX = CGFloat( currentIndex)*collectionView.frame.width
        collectionView.setContentOffset(CGPoint(x:offsetX,y:0), animated: true)
    }
}
